﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                FillData();
            }
        }

        private void FillData()
        {
            var dt = Dbhelper.GetDataTable("select * from Articles");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
      
            public string GetCellValues(int rowIndex,int CellIndex)
        {
            var control = GridView1.Rows[rowIndex].Cells[CellIndex];
            var result = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;
            return result;

        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillData();          
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idstring = GetCellValues(e.RowIndex, 0);
            var title = GetCellValues(e.RowIndex, 1);
            var content = GetCellValues(e.RowIndex, 2);
            var author = GetCellValues(e.RowIndex, 3);
            var remarks = GetCellValues(e.RowIndex, 4);
            var isok = int.TryParse(idstring, out int id);
            if (isok)
            {
                var sql = string.Format("update Articles set Title='{0}',Content='{1}',Author='{2}',Remarks='{3}' where Id='{4}'", title, content, author, remarks, id);
                Dbhelper.AddOrUpdateOrDelete(sql);
                GridView1.EditIndex = -1;
                FillData();
            }
            else
            {
                var sqls = string.Format("insert into Articles(Title,Content,Author,Remarks) values('{0}','{1}','{2}','{3}')", title, content, author, remarks);
                Dbhelper.AddOrUpdateOrDelete(sqls);
                GridView1.EditIndex = -1;
                FillData();
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetCellValues(e.RowIndex, 0);
            var sc = string.Format("delete from Articles where Id ='{0}'", id);
            Dbhelper.AddOrUpdateOrDelete(sc);
            GridView1.EditIndex = -1;
            FillData();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var temp = Dbhelper.GetDataTable(sql);

            var dt = temp.Copy();
            dt.Rows.Add(dt.NewRow());

            GridView1.EditIndex = dt.Rows.Count - 1;
            GridView1.DataSource = dt;
            GridView1.DataBind();
            
        }
    }
}